import {
  Injectable,
  ClassSerializerInterceptor,
  PlainLiteralObject,
  StreamableFile,
} from '@nestjs/common';
import { ClassTransformOptions, classToPlain } from 'class-transformer';
import { isObject } from 'lodash';
import { isArray } from 'class-validator';
@Injectable()
export class LoggingInterceptor extends ClassSerializerInterceptor {
  serialize(
    response: PlainLiteralObject | Array<PlainLiteralObject>,
    options: ClassTransformOptions,
  ): PlainLiteralObject | PlainLiteralObject[] {
    if (
      (!isObject(response) && !isArray(response)) ||
      response instanceof StreamableFile
    ) {
      return response;
    }

    // 如果是响应数据是数组,则遍历对每一项进行序列化
    // if (isArray(response)) {
    //   return (response as PlainLiteralObject[]).map((item) =>
    //     !isObject(item) ? item : this.transformToPlain(response, options),
    //   );
    // }

    // 如果是分页数据,则对items中的每一项进行序列化
    if ('meta' in response && 'items' in response) {
      const items =
        !response.items && isArray(response.items) ? response.items : [];
      return {
        ...response,
        items: (items as PlainLiteralObject[]).map((item) => {
          return !isObject(item)
            ? item
            : this.transformToPlain(response, options);
        }),
      };
    }
    // 如果响应是个对象则直接序列化
    return this.transformToPlain(response, options);
  }
}
